package com.shdaily.android;

import java.util.ArrayList;  
import java.util.List;  
import android.content.Context;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.view.View.OnClickListener;  
import android.view.LayoutInflater;
import android.widget.*;
import android.net.Uri;
import com.shdaily.android.*;


public class TopNewsAdapter extends BaseAdapter {
	
	private Context myContext;  

	public List<Article> articleList;  
	private Article article;  
	private EngineDB db;  
	//private int dWidth;  
	
	public TopNewsAdapter (Context context){  
			myContext = context;  
			//dWidth = displayWidth;  
			db = new EngineDB(myContext);  
			articleList = new ArrayList<Article>();  
			articleList = db.getTopNewsList();  
	}  

	public int getCount(){  
		return articleList.size();  

	}  

	public Object getItem(int position) {  
		return position;  

	}  

	public long getItemId(int position) {  
		return position;  
	}  

	/*public View getView(int position,View convertView, ViewGroup parent){  
		article = articleList.get(position);  
		ArticleView articleView = new ArticleView(myContext, article,dWidth);  
		articleView.buttonFavorite.setOnClickListener(new OnClickListener() {  
			public void onClick(View v) {  
				EngineDB db = new EngineDB(myContext);  
				ArticleView art= (ArticleView)v.getParent();   
				db.delete(art.article);  
				refresh();  
			}  

		});  
		return articleView;  
	}  */
	
	public View getView(int position, View convertView, ViewGroup parent) 
	{     
		//get the Article object for each individual row position
		article = articleList.get(position);

		if (article.getHasPic()<=0) {
			// if article has no picture, no thumbnail image should be displayed, use a LinearLayout
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			LinearLayout view = (LinearLayout)inflater.inflate(R.layout.listitemnoimg, null, false);
			TextView name = (TextView) view.findViewById(R.id.result_title);
			name.setText(article.getTitle());
			TextView secondLine = (TextView) view.findViewById(R.id.result_summary);
			secondLine.setText(article.getSummary());
			return view;
			
		}
		else {
			// if article has no picture, use a RelativeLayout to display thumbnail along with the title & summary
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.listitem, null, false);
			ImageView image = (ImageView) view.findViewById(R.id.result_image);
			image.setImageURI(Uri.parse(article.findThumbnail().getURL()));
			TextView name = (TextView) view.findViewById(R.id.result_title);
			name.setText(article.getTitle());
			TextView secondLine = (TextView) view.findViewById(R.id.result_summary);
			secondLine.setText(article.getSummary());
			TextView thirdLine = (TextView) view.findViewById(R.id.result_timeago);
			thirdLine.setText(article.getTimeAgo());
			return view;
		}
	} 

	public void refresh(){  
		articleList = new ArrayList<Article>();  
		FeedHandler handler = new FeedHandler("", 20, 1, 0);
        handler.updateArticles(myContext);
        articleList = handler.getArticles();
		//articleList = db.getTopNewsList();  
		notifyDataSetChanged();  
	}  


}
